package com.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.mapper.MemberMapper;
import com.health.pojo.Member;
import com.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    /**
     * 会员统计
     *
     * @return
     */
    @Override
    public Map<String, Object> getMemberReportData() {

        List<String> dates = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();

        for (int i = 11; i >= 0; i--) {
            LocalDate now = LocalDate.now()
                    .plusMonths(-i);
            now = LocalDate.of(now.getYear(), now.getMonth(), now.lengthOfMonth());
            Long count = this.query()
                    .lt("reg_time", now)
                    .count();
            dates.add(now.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            counts.add(count.intValue());
        }

        return Map.of("months", dates, "memberCount", counts);
    }
}
